package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.PlanFundsModel;
import com.app.model.StriveFundsModel;
import com.app.service.StriveForFundsService;
import com.app.vo.FundsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 争取资金
 */
@Controller
@RestController
@RequestMapping("/api/funds")
public class StriveForFundsController {
    @Autowired
    private StriveForFundsService striveForFundsService;

    //查询
    @GetMapping("/all")
    public Result<Object> getAll( String status, Integer sort, String year) {
        PlanFundsModel model = new PlanFundsModel();
        model.setStatusText(status);
        model.setSort(sort);
        model.setYear(year);
        return Result.success(striveForFundsService.fundsList(model));
    }


    //争取资金后台管理
    //计划列表
    //查询列表
    @GetMapping("/selectPlan")
    public Result<Object> selectPlan(int page, int limit, String year) {
        PlanFundsModel model = new PlanFundsModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setYear(year);
        return Result.success(striveForFundsService.selectPlan(model));
    }

    //保存
    @PostMapping("/savePlan")
    public Result<Object> savePlan(@RequestBody JSONObject json) {
        String year = json.get("year").toString();
        JSONArray jsonArray = JSONArray.parseArray(json.get("multiple").toString());
        PlanFundsModel model = new PlanFundsModel();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject multiple = jsonArray.getJSONObject(i);
            String taskPlan = multiple.get("taskPlan").toString();
            if (taskPlan == null || Double.valueOf(taskPlan) < 0) {
                return Result.success("年度任务不能小于0");
            }
            Integer departmentId = (Integer) multiple.get("departmentId");
            Integer status = (Integer) multiple.get("status");
            model.setStatus(status);
            model.setDepartmentId(departmentId);
            if (Double.valueOf(taskPlan)!=0) {
                model.setTaskPlan(Double.valueOf(taskPlan));
            }
            model.setYear(year);
            model.setCreateTime(LocalDateTime.now());
            model.setUpdateTime(LocalDateTime.now());
            striveForFundsService.savePlan(model);
        }
        return Result.success("操作成功");
    }

    //完成情况
    // 查询列表
    @GetMapping("/completion")
    public Result<Object> selectCompletion(int page, int limit, String year, String keyword) {
        StriveFundsModel model = new StriveFundsModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);

        if (year == null || year.equals("")) {
            FundsVo vo = striveForFundsService.selectCompletion(model);
            return Result.success(vo);
        }
        model.setYear(year);
        return Result.success(striveForFundsService.selectCompletionByTime(model));
    }

    //添加/编辑
    @PostMapping("/add")
    public Result<Object> add(@RequestBody StriveFundsModel model) {
        model.setCreateTime(LocalDateTime.now());
        striveForFundsService.add(model);
        return Result.success("添加成功");
    }

    @PostMapping("/update")
    public Result<Object> update(@RequestBody StriveFundsModel model) {
        model.setUpdateTime(LocalDateTime.now());
        striveForFundsService.updateFunds(model);
        return Result.success("编辑成功");
    }

    //删除
    @GetMapping("/delete")
    public Result<Object> delete(int id) {
        striveForFundsService.delete(id);
        return Result.success("删除成功");
    }

    //状态列表/api/funds/getStatusList
    @GetMapping("/getStatusList")
    public Result<Object> getStatusList() {
        return Result.success(striveForFundsService.getStatusList());
    }
}
